package ${package.Controller};

import com.github.pagehelper.PageInfo;
import com.sinoits.microservicehlyy.conf.log.InterfaceLog;
import com.sinoits.microservicehlyy.conf.executiontime.ExecutionTime;
import com.sinoits.microservicehlyy.dto.ParamGroupDTO;
import com.sinoits.microservicehlyy.dto.input.${entity}InputDTO;
import com.sinoits.microservicehlyy.dto.input.Select${entity}InputDTO;
import com.sinoits.microservicehlyy.dto.output.BaseOutputDTO;
import com.sinoits.microservicehlyy.dto.output.${entity}OutputDTO;
import com.sinoits.microservicehlyy.enums.CodeEnum;
import com.sinoits.orm.pojo.${entity}DoWithBlobs;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>

import org.springframework.web.bind.annotation.*;

import java.util.Objects;
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * ${table.comment!}
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@Api("${table.comment!}")
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Resource
    private ${entity}Service ${entity?uncap_first}Service;


    @ApiOperation("添加${table.comment!}")
    @ExecutionTime
    @InterfaceLog(name = "添加${table.comment!}")
    @PostMapping(value = "insert${entity}")
    public BaseOutputDTO insert${entity}(
            @Validated @RequestBody ${entity}InputDTO inputDTO) {
        BaseOutputDTO baseOutputDTO = new BaseOutputDTO();

        try {
            int success = 0;

            Integer i = ${entity?uncap_first}Service.insert${entity}(inputDTO);
            if (i < success) {

            baseOutputDTO.setCode(CodeEnum.CodeType.添加失败.index);
            baseOutputDTO.setSuccess(false);
            baseOutputDTO.setMessage("添加失败");
            } else {
            baseOutputDTO.setCode(CodeEnum.CodeType.成功.index);
            baseOutputDTO.setSuccess(true);
            baseOutputDTO.setMessage("成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            baseOutputDTO.setSuccess(false);
            baseOutputDTO.setMessage(e.toString());
            baseOutputDTO.setCode(CodeEnum.CodeType.应用程序异常.index);
        }

        return baseOutputDTO;
    }

    @ApiOperation("修改${table.comment!}")
    @ExecutionTime
    @InterfaceLog(name = "修改${table.comment!}")
    @PostMapping(value = "update${entity}")
    public BaseOutputDTO update${entity}(@Validated(value = {ParamGroupDTO.Update.class})
                                         @RequestBody ${entity}InputDTO inputDTO) {
        BaseOutputDTO baseOutputDTO = new BaseOutputDTO();

        try {
            int success = 0;

            Integer i = ${entity?uncap_first}Service.update${entity}(inputDTO);
            if (i < success) {

                baseOutputDTO.setCode(CodeEnum.CodeType.修改失败.index);
                baseOutputDTO.setSuccess(false);
                baseOutputDTO.setMessage("修改失败");
            } else {
                baseOutputDTO.setCode(CodeEnum.CodeType.成功.index);
                baseOutputDTO.setSuccess(true);
                baseOutputDTO.setMessage("成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            baseOutputDTO.setSuccess(false);
            baseOutputDTO.setMessage(e.toString());
            baseOutputDTO.setCode(CodeEnum.CodeType.应用程序异常.index);
        }

        return baseOutputDTO;
    }

    @ApiOperation("删除${table.comment!}")
    @ExecutionTime
    @InterfaceLog(name = "删除${table.comment!}")
    @Valid
    @GetMapping(value = "delete${entity}")
    public BaseOutputDTO delete${entity}(
    @RequestParam("id") @NotNull(message = "id不能为空") Integer id) {
        BaseOutputDTO baseOutputDTO = new BaseOutputDTO();

        try {
            int success = 0;

            Integer i = ${entity?uncap_first}Service.delete${entity}(id);
            if (i < success) {

                baseOutputDTO.setCode(CodeEnum.CodeType.修改失败.index);
                baseOutputDTO.setSuccess(false);
                baseOutputDTO.setMessage("删除失败");
            } else {
                baseOutputDTO.setCode(CodeEnum.CodeType.成功.index);
                baseOutputDTO.setSuccess(true);
                baseOutputDTO.setMessage("成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            baseOutputDTO.setSuccess(false);
            baseOutputDTO.setMessage(e.toString());
            baseOutputDTO.setCode(CodeEnum.CodeType.应用程序异常.index);
        }

        return baseOutputDTO;
    }

    @ApiOperation("查询${table.comment!}列表")
    @ExecutionTime
    @InterfaceLog(name = "查询${table.comment!}列表")
    @GetMapping(value = "select${entity}List")
    public BaseOutputDTO< ${entity}OutputDTO > select${entity}List(
            @Validated Select${entity}InputDTO inputDTO) {

        BaseOutputDTO< ${entity}OutputDTO > baseOutputDTO = new BaseOutputDTO<>();
        ${entity}OutputDTO outputDTO = new ${entity}OutputDTO();
        try {

            PageInfo< ${entity}DoWithBlobs > pageInfo = ${entity?uncap_first}Service
                    .select${entity}List(inputDTO);
            if (Objects.isNull(pageInfo) || CollectionUtils.isEmpty(pageInfo.getList())) {
                baseOutputDTO.setSuccess(false);
                baseOutputDTO.setMessage("数据为空");
                baseOutputDTO.setCode(CodeEnum.CodeType.没有数据.index);
                return baseOutputDTO;
            }
            outputDTO.set${entity}DoList(pageInfo.getList());
            outputDTO.setTotal(pageInfo.getTotal());
            baseOutputDTO.setData(outputDTO);
            baseOutputDTO.setSuccess(true);
            baseOutputDTO.setMessage("查询成功");
            baseOutputDTO.setCode(CodeEnum.CodeType.成功.index);

        } catch (Exception e) {
            e.printStackTrace();
            baseOutputDTO.setSuccess(false);
            baseOutputDTO.setMessage(e.toString());
            baseOutputDTO.setCode(CodeEnum.CodeType.应用程序异常.index);
        }

        return baseOutputDTO;
    }


}